/*
 * Created on 2004-12-7
 */
package com.tss.util;

import java.sql.Date;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Vector;

import com.jedi.FormatDate;

/**
 * @author DaiRongBang
 *
 * ��¼�����󣬽�ResultSet���������ת���ɴ˶����������Vector���󣬲�����4������
 */
public class DbRs extends Vector{
	private Hashtable htColumn = new Hashtable();
	private Hashtable info = new Hashtable();
	public DbRs() {}
	
	public void put(String key, String value) 
	{
		htColumn.put(key, value);
	}

	public void printColumnInfo() 
	{
		Log.trace("Column Info:" + htColumn);
	}
	
	public void setInfo(String key, Object info) 
	{
		this.info.put(key, info);
	}
	
	public Object getInfo(String key) 
	{
		if (info.containsKey(key)) return info.get(key);
		return null;
	}
	
	public int getColumnIndex(String column_name) throws SQLException
	{
		String ind = (String) htColumn.get(column_name.toUpperCase());
		if (ind == null) 
		{
			throw new SQLException(DbConn.RsFieldNotFound.replaceAll("&name",column_name));
		}
		else 
		{
			return Integer.parseInt(ind);
		}
	}

	public int getInt(int row,int col) throws SQLException{
		String r = get(row,col);
		try {
			return Integer.parseInt(r);
		}
		catch(Exception ex) {
			return 0;
		}
	}
	public Date getDate(int row,String col) throws SQLException{
		String r = get(row,col);
		try {
			FormatDate formatDate = new FormatDate();
			return formatDate.toDate(r);
		}
		catch(Exception ex) {
			return null;
		}
	}
	
	public double getDouble(int row,int col) throws SQLException{
		String r = get(row,col);
		try {
			return Double.parseDouble(r);
		}
		catch(Exception ex) {
			return 0.00;
		}
	}

	public int getInt(int row,String column_name) throws SQLException{
		return getInt(row,getColumnIndex(column_name));
	}
	public boolean getBoolean(int row,String column_name) throws SQLException{
		String r= get(row, getColumnIndex(column_name));
		
		if (r.equals("true")) {return true;}
		else {return false;}
	}
	
	public double getDoulbe(int row,String column_name) throws SQLException{
		return getDouble(row,getColumnIndex(column_name));
	}
	public long getLong(int row,int col) throws SQLException{
		String r = get(row,col);
		try {
			return Long.parseLong(r);
		}
		catch(Exception ex) {
			return 0;
		}
	}
	
	public long getLong(int row,String column_name) throws SQLException{
		return getLong(row,getColumnIndex(column_name));
	}
	
	public String get(int row, int col) throws SQLException
	{
		if (isEmpty() || size()<row)
			throw new SQLException(DbConn.RsFieldNotFound.replaceAll("&name",row + ""));
		Vector vRow = (Vector)elementAt(row);
		if (vRow.isEmpty() || vRow.size()<col)
			throw new SQLException(DbConn.RsFieldNotFound.replaceAll("&name",row + ""));
		
		return vRow.elementAt(col) == null? "" : vRow.elementAt(col).toString();
	}

	public String get(int row, String column_name) throws SQLException
	{
		return get(row, getColumnIndex(column_name));
	}

	public Vector getRow(int row) 
	{
		if (getRowCount()<row) return null;
		return (Vector) elementAt(row);
	}

	public Vector getColumn(int col) throws SQLException
	{
		if (isEmpty() || size()<=0)
			return null;
		Vector ret = new Vector();
		if (col<0 || ((Vector)elementAt(0)).isEmpty() || ((Vector)elementAt(0)).size()<col)
			throw new SQLException(DbConn.RsFieldNotFound.replaceAll("&name",col + ""));
		for (int i = 0; i < getRowCount(); i++) 
		{
			ret.add(((Vector)elementAt(i)).elementAt(col));
		}
		return ret;
	}

	public Vector getColumn(String colName) throws SQLException
	{
		return getColumn(getColumnIndex(colName));
	}

	public int getRowCount() 
	{
		return size();
	}

	public int getColCount() 
	{
		if (isEmpty() || size() <= 0)
			return 0; 
		return ( (Vector) elementAt(0)).size();
	}
}
